//
//  OllViewController.m
//  Article
//
//  Created by LONG on 16/5/6.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "OllViewController.h"
#import "MaterTableViewCell.h"
#import "MeansViewController.h"
#import "MaterModel.h"

@interface OllViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray *dataarr;

@end

@implementation OllViewController{
    UITableView *_myTableView;
    __block int page;
    UILabel *_titlelabel;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorFromRGB(0xefeff1);
    page = 1;
    [self requestTableview];
    [self myTableView];

    // Do any additional setup after loading the view.
}

- (UITableView *)myTableView{
    if (nil == _myTableView){
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-95)];
       // _myTableView.backgroundColor = [UIColor whiteColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        [_myTableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(downMJ)];
        [_myTableView registerNib:[UINib nibWithNibName:@"MaterTableViewCell" bundle:nil] forCellReuseIdentifier:@"MaterTableViewCell"];
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
        headerView.backgroundColor = UIColorFromRGB(0xefeff1);
        [self.view addSubview:headerView];
        
        _titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, WIDTH, 30)];
        _titlelabel.backgroundColor = UIColorFromRGB(0xffffff);
        _titlelabel.text = [NSString stringWithFormat:@"   我的全部成员(%zd)",self.dataarr.count];
        _titlelabel.font = [UIFont fontWithName:@"Heiti SC" size:14 *MYWIDTH];
        [headerView addSubview:_titlelabel];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 3, 20)];
        label.backgroundColor = UIColorFromRGB(MYColor);
        [_titlelabel addSubview:label];
        _myTableView.tableHeaderView = headerView;
        
        [self setExtraCellLineHidden:_myTableView];
        
        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}
//下拉刷新
-(void)downMJ{
    page = 1;
    [self requestTableview];
    
    [_myTableView.header endRefreshing];
}
//上拉加载
- (void)loadMoreData{
    page++;
    [self requestTableview];
    [_myTableView.footer endRefreshing];
    
}
- (void)requestTableview{
    if (_dataarr == nil || page == 1) {
        _dataarr = [NSMutableArray arrayWithCapacity:0];;
        
    }
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];

    NSDictionary *dic = @{@"user_token":[user objectForKey:@"token"],
                          @"page":[NSString stringWithFormat:@"%d",page]};
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:EMPLOYEE_URL parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            //建立模型
            for (NSDictionary*dic in [responseObject objectForKey:@"data"]) {
                MaterModel*model=[[MaterModel alloc]init];
                
                [model setValuesForKeysWithDictionary:dic];
                [model setValuesForKeysWithDictionary:[dic objectForKey:@"userinfo"]];

                //追加数据
                [self.dataarr addObject:model];
            }
            _titlelabel.text = [NSString stringWithFormat:@"   我的全部成员(%@)",[responseObject objectForKey:@"count"]];

        }else{
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
        }
        if (_dataarr.count > 8) {
            [_myTableView addGifFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        }
        [_myTableView reloadData];
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}
#pragma mark tableView代理

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_dataarr.count<=0) {
        return 0;
    }
    return self.dataarr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MaterTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MaterTableViewCell"];
    if (!cell) {
        cell=[[MaterTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MaterTableViewCell"];
        
    }
    if (_dataarr.count<=0) {
        return cell;
    }
    MaterModel*model = self.dataarr[indexPath.row];
    [cell configModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
    
}

#pragma mark delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 80;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    

    MeansViewController *mean = [[MeansViewController alloc]init];
    MaterModel*model = self.dataarr[indexPath.row];

    mean.uid = model.uid;
    [self.navigationController pushViewController:mean animated:YES];
}

-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}

@end
